package com.ruoyi.web.controller.tool;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.fs.domain.*;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ExcelExporter {
    public static void exportDataToTemplate(
            HttpServletResponse response, List<FsCase> list, InputStream inputStream,
            List<SysUser> users, List<FsAddress> addresses) throws IOException {
        Map<Long, String> userMap = new HashMap<>();
        Map<Long, String> addressMap = new HashMap<>();
        for (int i = 0; i < users.size(); i++) {
            userMap.put(users.get(i).getUserId(), users.get(i).getNickName());
        }
        for (int i = 0; i < addresses.size(); i++) {
            addressMap.put(addresses.get(i).getId(), addresses.get(i).getName());
        }
        XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
        Sheet sheet = workbook.getSheetAt(0);
        int startRow = 3;
        for (int i = 0; i < list.size(); i++) {
            FsCase fsCase = list.get(i);
            FsClient bg = fsCase.getBgDsr();
            Row row = sheet.createRow(startRow + i);

            Cell zf = row.createCell(0);
            zf.setCellValue(fsCase.getZfName());
            Cell batch = row.createCell(1);
            batch.setCellValue(fsCase.getBatchName());
            Cell ls = row.createCell(2);
            ls.setCellValue(fsCase.getOwner());
            Cell tjy = row.createCell(3);
            tjy.setCellValue(userMap.get(fsCase.getTjyId()));
            Cell sf = row.createCell(4);
            sf.setCellValue(addressMap.get(fsCase.getSfid()));
            Cell cs = row.createCell(5);
            cs.setCellValue(addressMap.get(fsCase.getSid()));
            Cell qx = row.createCell(6);
            qx.setCellValue(addressMap.get(fsCase.getQxid()));
            Cell fy = row.createCell(7);
            fy.setCellValue(fsCase.getFymc());


            Cell ajh = row.createCell(8);
            ajh.setCellValue(fsCase.getNbajbh());
            Cell xm = row.createCell(9);
            xm.setCellValue(bg.getXm());
            Cell lasj = row.createCell(10);
            String cjsj = fsCase.getCjsj();
            lasj.setCellValue(DateUtils.formatChange("yyyy-MM-dd HH:mm:ss", "yyyy/MM/dd", cjsj));
            Cell lafs = row.createCell(11);
            String lafsStr = "网上立案";
            if (fsCase.getLafs() == 1) {
                lafsStr = "邮寄立案";
            }
            lafs.setCellValue(lafsStr);
            Cell fksj = row.createCell(12);
            String gxsj = fsCase.getGxsj();
            fksj.setCellValue(DateUtils.formatChange("yyyy-MM-dd HH:mm:ss", "yyyy/MM/dd", gxsj));
            Cell sljg = row.createCell(13);
            int zt = fsCase.getZt();
            String sljgStr = "";
            String shjg = "";
            if (zt == 3 || zt == 5 || zt == 31) {
                sljgStr = "不通过";
                List<FsCaseExamine> examineList = fsCase.getExamineList();
                if (!examineList.isEmpty()) {
                    FsCaseExamine last = examineList.get(examineList.size() - 1);
                    shjg = last.getShyj();
                    int bh = shjg.indexOf(";驳回意见");
                    if (bh > 0) {
                        shjg = shjg.substring(0, bh);
                    }
                    shjg = shjg.replace("审核意见：", "");
                }

            }
            if (zt == 1) {
                sljgStr = "待审查";
            } else if (zt == 2) {
                sljgStr = "审查通过";
            } else if (zt == 3) {
                sljgStr = "审查不通过";
            } else if (zt == 4) {
                sljgStr = "已立案";
            } else if (zt == 5) {
                sljgStr = "立案不通过";
            } else if (zt == 31) {
                sljgStr = "待补正";
            }
            sljg.setCellValue(sljgStr);
            Cell bysl = row.createCell(14);
            if (StringUtils.isNotBlank(shjg)) {
                bysl.setCellValue(shjg);
            }
        }
        workbook.write(response.getOutputStream());
    }

    public static void exportDataToTemplateForRepaymentSchedule(
            HttpServletResponse response, List<FsCaseRepaymentSchedule> list, InputStream inputStream) throws IOException {
        XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
        Sheet sheet = workbook.getSheetAt(0);
        int startRow = 1;
        for (int i = 0; i < list.size(); i++) {
            FsCaseRepaymentSchedule schedule = list.get(i);
            FsCase fsCase = schedule.getFsCase();
            FsClient bg = fsCase.getBgDsr();
            Row row = sheet.createRow(startRow + i);

            Cell nbbh = row.createCell(0);
            nbbh.setCellValue(fsCase.getNbajbh());
            Cell zf = row.createCell(1);
            zf.setCellValue(fsCase.getZfName());
            Cell batch = row.createCell(2);
            batch.setCellValue(fsCase.getBatchName());
            Cell sszt = row.createCell(3);
            sszt.setCellValue(fsCase.getSsztmc());
            Cell xm = row.createCell(4);
            xm.setCellValue(bg.getXm());
            Cell dh = row.createCell(5);
            dh.setCellValue(bg.getSjhm());

            Cell jhrq = row.createCell(6);
            jhrq.setCellValue(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, schedule.getJhrq()));
            Cell jhje = row.createCell(7);
            jhje.setCellValue(String.valueOf(schedule.getJhje().setScale(2, RoundingMode.HALF_UP)));
            Cell zt = row.createCell(8);
            String ztMc = "";
            if (schedule.getZt() == 0) {
                ztMc = "待还款";
            } else if (schedule.getZt() == 1) {
                ztMc = "部分还款";
            } else if (schedule.getZt() == 2) {
                ztMc = "已还清";
            } else if (schedule.getZt() == 3) {
                ztMc = "已逾期";
            }
            zt.setCellValue(ztMc);
            Cell tjy = row.createCell(9);
            tjy.setCellValue(fsCase.getTjy());

            Cell yhje = row.createCell(10);
            yhje.setCellValue(String.valueOf(schedule.getYhje().setScale(2, RoundingMode.HALF_UP)));
            Cell dshje = row.createCell(11);
            dshje.setCellValue(String.valueOf(schedule.getDshje().setScale(2, RoundingMode.HALF_UP)));
            Cell bz = row.createCell(12);
            bz.setCellValue(schedule.getRemark());
        }
        workbook.write(response.getOutputStream());
    }

    public static void exportDataToTemplateForRepaymentScheduleSh(
            HttpServletResponse response, List<FsCaseRepaymentSchedulePay> list, InputStream inputStream) throws IOException {
        XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
        Sheet sheet = workbook.getSheetAt(0);
        int startRow = 1;
        for (int i = 0; i < list.size(); i++) {
            FsCaseRepaymentSchedulePay pay = list.get(i);
            FsCaseRepaymentSchedule schedule = pay.getSchedule();
            FsCase fsCase = schedule.getFsCase();
            FsClient bg = fsCase.getBgDsr();
            Row row = sheet.createRow(startRow + i);

            Cell nbbh = row.createCell(0);
            nbbh.setCellValue(fsCase.getNbajbh());
            Cell zf = row.createCell(1);
            zf.setCellValue(fsCase.getZfName());
            Cell batch = row.createCell(2);
            batch.setCellValue(fsCase.getBatchName());
            Cell sszt = row.createCell(3);
            sszt.setCellValue(fsCase.getSsztmc());
            Cell tjy = row.createCell(4);
            tjy.setCellValue(fsCase.getTjy());
            Cell xm = row.createCell(5);
            xm.setCellValue(bg.getXm());
            Cell dh = row.createCell(6);
            dh.setCellValue(bg.getSjhm());

            Cell jhrq = row.createCell(7);
            jhrq.setCellValue(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, schedule.getJhrq()));
            Cell jhje = row.createCell(8);
            jhje.setCellValue(String.valueOf(schedule.getJhje().setScale(2, RoundingMode.HALF_UP)));
            Cell shrq = row.createCell(9);
            shrq.setCellValue(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, pay.getShrq()));
            Cell shje = row.createCell(10);
            shje.setCellValue(String.valueOf(pay.getShje().setScale(2, RoundingMode.HALF_UP)));
            Cell zt = row.createCell(11);
            String ztMc = "";
            if (pay.getZt() == 0) {
                ztMc = "待审核";
            } else if (pay.getZt() == 1) {
                ztMc = "已审核";
            }
            zt.setCellValue(ztMc);

            Cell bz = row.createCell(12);
            bz.setCellValue(schedule.getRemark());
        }
        workbook.write(response.getOutputStream());
    }

}
